package com.wawj.compplugin

import groovy.swing.SwingBuilder
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction

import javax.inject.Inject
import javax.swing.JLabel

abstract class NewBranchTask extends DefaultTask {
    @Inject
    NewBranchTask() {
        group = "wawj"
        description = '新建分支，并修改指向'
    }
    @Input
    def newBranchName = ""
    @Input
    def isContinue = false


    @TaskAction
    void newBranch() {
        //首先找出现包含哪些仓库
        //这些仓库有没有未提交的代码
        //如果有提醒，并中断
        //如果没有，则弹框输入分支名

        //判断这些仓库分支是否已存在
        //存在，中断

        showNoConfigFileDialog()


        if (isContinue) {
            //判断这些仓库分支是否已存在
            println("新分支名" + newBranchName)

        } else {
            //中断
            throw new RuntimeException("取消新建分支")
        }
    }


    def showNoConfigFileDialog() {
        if (System.console() == null) {
            System.setProperty('java.awt.headless', 'false')
            new SwingBuilder().edt {
                dialog(modal: true, size: [400, 230], title: '创建分支', alwaysOnTop: true, resizable: true, locationRelativeTo: null, pack: false, show: true) {
                    vbox() {
                        label(text: " ")
                        label(text: "为xxx、xxx仓库和主仓库新建分支", horizontalAlignment: JLabel.LEFT)
                        label(text: " ")
                        label(text: "并自动修改这些仓库的分支指向")
                        label(text: " ")
                        label(text: "请输入新分支名")
                        def input = textField()
                        hbox() {
                            button(defaultButton: true, text: '确定', actionPerformed: {
                                dispose()
                                newBranchName = input.text
                                isContinue = true
                            })
                            label(text: "")
                            button(defaultButton: false, text: '取消', actionPerformed: {
                                dispose()
                                isContinue = false
                            })
                        }
                        label(text: " ")
                    }
                }
            }
        } else {
            isContinue = false
        }
    }

}